function wMval = calculateWMval(U_, S_)

kmat=[1 1; 1 2; 2 1; 2 2];

%    lb = [-0.1 0.75 1];
%    ub = [0.1 1.33 1];
    lb = [-0.1 0.5 1];
    ub = [0.1 2 1];
    
for k = size(kmat, 1):-1:1
    v1 = U_{1}(:, kmat(k, 1));
    v2 = U_{2}(:, kmat(k, 2)); 
    

    options = optimoptions('particleswarm','SwarmSize',40,'UseParallel',true,'Display','off');
        cn = particleswarm(@(c) opt_dist(v1,-v2,c(1:2)), 2, lb(1:2), ub(1:2),options);
        cp = particleswarm(@(c) opt_dist(v1,v2,c(1:2)), 2, lb(1:2), ub(1:2),options);
    
    if opt_dist(v1, -v2, cn) < opt_dist(v1, v2, cp)
        copt{k}(1,1, :) = cn;
        nflag = -1;
        cO = cn;
    else
        copt{k}(1, 1, :) = cp;
        cO = cp;
        nflag = 1;
    end
    D(k) = opt_dist(v1, v2*nflag, cO);
   
end
        
        S(:,1) = S_{1}(1:2);
        S(:,2) = S_{2}(1:2);
        S(2,:) = S(2,:) - S(1,:);
        
        if D(1) + D(4) > D(2) + D(3)
            m1 = D(2);
            m2 = D(3);            
            S1 = S(1,kmat(2,1)) + S(2,kmat(2,2));
            S2 = S(2,kmat(3,1)) + S(2,kmat(3,2));
            wMval = (S1*m1 + S2*m2)/(S1+S2);
        else
            m1 = D(1);
            m2 = D(4);
            
            S1 = S(1,kmat(1,1)) + S(2,kmat(1,2));
            S2 = S(2,kmat(4,1)) + S(2,kmat(4,2));
            
            wMval = (S1*m1 + S2*m2)/(S1+S2);
            
        end
        
        
end